﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CloudSoft.Extensions;

namespace ERPStore.Exporter.Providers
{
	public class TouslesprixExport : ExportBase
	{
		private StringBuilder m_Document;

		public TouslesprixExport()
			: base()
		{
		}

		public override void InitializeSettings(Models.Settings settings)
		{
			settings.AllowMissingDefaultImage = false;
			settings.AllowNotSellable = false;
			settings.AllowNullLongDescription = true;
			settings.AllowNullShortDescription = true;
			settings.AllowStockEmpty = true;
			settings.AllowZeroSalePrice = false;
			settings.FileName = "touslesprix.txt";
		}

		public override string LeadSourceId
		{
			get
			{
				return "F4B58DBF-CEF6-49C8-8D82-947CC2770C35";
			}
		}

		public override string Name
		{
			get { return "touslesprix"; }
		}

		public override void CreateDocument()
		{
			m_Document = new System.Text.StringBuilder();
			m_Document.AppendLine("identifiant|Marque|Nom|Description|Url|image|DelaiLivraison|FraisLivraison|PrixTTC|Ecotaxe|categorie|ean|ref_constructeur");
		}

		public override void AddElementToDocument(ERPStore.Models.Product product, ERPStore.Models.ProductStockInfo productStock)
		{
			var categorie = GetDeepCategoryName(product);
			var identifiant = product.Code;
			var nom = (product.PageTitle ?? product.Title).Replace("|", " ");
			var description = product.ShortDescription.DefaultValueIfNullOrTrimmedEmpty(product.Title).Replace(System.Environment.NewLine, " ");
			var prixTTC = string.Format("{0:F2}", product.BestPrice.ValueWithTax);
			var url = BuildEncryptedProductUrl(LeadSource, product);
			var image = string.Format("http://{0}{1}", GlobalConfiguration.Configuration.Settings.DefaultUrl, string.Format(product.DefaultImage.Url, 0, 0));
			var defaultCountry = GlobalConfiguration.Configuration.Settings.Shipping.DeliveryCountryList[0];
			var fraislivraison = ToCurrency(new ERPStore.Models.Price(defaultCountry.ShippingFeeAmount, product.DefaultTaxRate).ValueWithTax,true);
			if (product.Shipping.Strategy == ERPStore.Models.FeeTransportStrategy.FixedPrice)
			{
				fraislivraison = ToCurrency(new ERPStore.Models.Price(product.Shipping.FixedPrice, product.DefaultTaxRate).ValueWithTax,true);
			}
			else if (product.BestPrice.Value > defaultCountry.MinimalFreeOfCarriageAmount)
			{
				fraislivraison = "0";
			}

			string delailivraison = string.Empty;
			if (productStock != null)
			{
				delailivraison = string.Format("{0} jours", (productStock.HasStock) ? 2 : Math.Max(2, productStock.DeliveryDaysCount));
			}
			else
			{
				delailivraison = "5 jours";
			}
			var garantie = (product.Warranty.IsNullOrTrimmedEmpty()) ? "0" : "1";
			var EcoTaxe = (product.RecyclePrice != null) ? ToCurrency(product.RecyclePrice.ValueWithTax,true) : "0";
			var marque = (product.Brand != null) ? product.Brand.Name : "";
			var EAN = product.Gtin.EAN;
			var ref_constructeur = product.ManufacturerPartNumber;

			m_Document.AppendFormat("{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}|{9}|{10}|{11}|{12}\r\n"
				, identifiant
				, marque
				, nom
				, description
				, url
				, image
				, delailivraison
				, fraislivraison
				, prixTTC
				, EcoTaxe
				, categorie
				, EAN
				, ref_constructeur
				);
		}

		public override void SaveDocument()
		{
			var path = Configuration.ConfigurationSettings.AppSettings["exportPath"];
			var fileName = System.IO.Path.Combine(path, Settings.FileName);
			if (System.IO.File.Exists(fileName))
			{
				System.IO.File.Delete(fileName);
			}

			var content = m_Document.ToString();
			System.IO.File.WriteAllText(fileName, content, System.Text.Encoding.UTF8);
		}

		public override void Dispose()
		{
			m_Document = null;
		}

		protected override string GetDeepCategoryName(ERPStore.Models.Product product)
		{
			var path = new List<string>();
			var c = product.Category;
			if (c != null)
			{
				return c.Name;
			}
			while (true)
			{
				if (c != null)
				{
					path.Add(c.Name);
				}
				else
				{
					break;
				}
				c = c.Parent;
			}
			if (path.IsNullOrEmpty())
			{
				return string.Empty;
			}

			path.Reverse();
			return path.JoinString(" > ");
		}

	}
}
